// mircoApp/order_pay_success/index.js
import Dialog from "../../../dist/dialog/dialog";
const skinBehavior = require("../../../utils/themeSkin/skinBehavior")
const app = getApp();
Page({

  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    order_status_name:"",
    order_status:null,
    imgPath: app.globalData.imgPath,
    goods_list:[],
    order_id:"",
    page:1,
    total_page:1,
    vertical:false,
    t_x:"0",
    imageUrl: false,
    title: "",
    show: false,
    path: "",

    list: [],
    order_height: '1200rpx',
    Height: 0,
    LastHeight: 0,


    cservice: "",
    service_QRcode: "",
    service_mobile: "",

    order_pay_id: "",


    _scroll_width:"702rpx",
    timeData: {},
  },
  open_customer(e) {
    let data = e.currentTarget.dataset.item;
    let json = {};
    if (data.page_type !== 'hotel' && data.page_type !== 'live-install') {
      json.plugin = "store";
      json.store_id = data.store.id;
    }
    app._getNetWork({
      url: app.getNetAddresss("member.service.api"),
      data: json,
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result !== 1) {
          return app.tips(msg);
        }

        this.setData({
          cservice: data.cservice,
          service_QRcode: data.service_QRcode,
          service_mobile: data.service_mobile,
          show: true
        })
      }
    })



  },
  navigation(event) {
    let latitude = event.currentTarget.dataset.latitude;
    let longitude = event.currentTarget.dataset.longitude;
    let storeName = event.currentTarget.dataset.storename;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      name: storeName,
      scale: 18
    });
  },
  call_phone(e) {
    let number = e.currentTarget.dataset.number;
    wx.makePhoneCall({
      phoneNumber: number,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  imgHeight: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    var imgh = e.detail.height; //图片高度
    var imgw = e.detail.width; //图片宽度
    var swiperH = winWid * imgh / imgw + "px"; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    if (parseInt(swiperH) > this.data.LastHeight) {
      this.setData({
        Height: 'height:'+swiperH, //设置高度
      });
      this.data.LastHeight = parseInt(swiperH);
      this.initElementHeight();
    }
  },

  onChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  sotre_handle(e) {
    let data = e.currentTarget.dataset.item;
    if (data.store.specify_show == 0) {
      wx.navigateTo({
        url: '/packageC/o2o/HomeSeller/HomeSeller?store_id=' + data.store.id,
      })

    } else {
      wx.navigateTo({
        url: '/packageC/o2o/HomeSeller/HomeSeller?fromHome=1&store_id=' + data.store.id,
      })

    }
  },
  group_open(e) {
    let data = e.currentTarget.dataset.item;
    app._getNetWork({
      url: app.getNetAddresss("plugin.fight-groups.frontend.controllers.team.index"),
      data: {
        team_id: data.team_id
      },
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result !== 1) {
          return app.tips(msg);
        }
        let store_id = data.store_id||'0';
        wx.navigateTo({
          url: '/packageA/detail_v2/detail_v2?id=' + data.has_one_group.id + '/'+ store_id +'&name=group'
        });
      }
    })

  },

  shareWeixin(e) {
    let data = e.currentTarget.dataset.item;
    console.log(data, "data===")
    let path = '/packageB/member/group/GroupDetail/GroupDetail?name=group&id=' + data.team_id;
    if (data.store_id) {
      path += '&store_id=' + data.store_id;
    }

    this.setData({
      title: data.has_one_group.desc_content,
      imageUrl: data.has_one_group.goods_thumb,
      path
    })

  },
  tips_handle() {
    Dialog.alert({
      message: '订单支付后赠送需要等订单支付后才发放，订单完成后赠送的需要等订单确认收货后才发放，每月1号赠送需要等每月1号才发放，奖励数量请以最终实际发放数量为准',
    }).then(() => {
      // on close
    });
  },

  goto(e) {
    let url = e.currentTarget.dataset.url;

    if (url == 'course') {
      let data = e.currentTarget.dataset.item;
      wx.navigateTo({
        url: '/packageA/member/course/CourseDetail/CourseDetail?goods_id=' + data.goods_id
      })


    } else if (url == 'hotel') {
      let data = e.currentTarget.dataset.item;
      wx.navigateTo({
        url: '/packageC/hotel/HotelHome/HotelHome?id=' + data.id
      })

    } else if (url == 'coupon_v2_detail') {
      wx.navigateTo({
        url: '/packageA/member/coupon_v2/coupon_v2',
      })
    } else if (url == 'goods') {
      let data = e.currentTarget.dataset.item;
      wx.navigateTo({
        url: '/packageA/detail_v2/detail_v2?id=' + data.id,
      })
    } else if (url == '/packageD/redPacket/coupon_share') {

      let data = e.currentTarget.dataset.item;
      wx.navigateTo({
        url: "/packageD/coupon_share/coupon_share?orderid=" + data.order_id
      })
    }  else {
      if (url) {
        wx.navigateTo({
          url
        })
      }


    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      order_pay_id: options.pay || ''
    });
    this.getData();
  },
  getData() {
    app._getNetWork({
      url: app.getNetAddresss("payment.success-page.index"),
      data: {
        order_pay_id: this.data.order_pay_id
      },
      success:async resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        wx.setNavigationBarColor({
          frontColor: '#ffffff',
          backgroundColor: this.data.themeBaseColor,
        });
        if (result !== 1) {
          return app.tips(msg);
        }
        let json = {
          '-1':'已关闭',
          '0':"未支付",
          "1":"已支付",
          "2":"已发货",
          "3":"交易完成"
        }
        let order_status_name = json[data[0].public_param.order_status];
        let order_status =  data[0].public_param.order_status;
        wx.setNavigationBarTitle({title:order_status_name})
        this.setData({
          list: data,
          order_id:data[0].public_param.order_id,
          order_status_name,
          order_status
        });
        console.log(data,"data====");
        if(data[0].public_param.order_id&&data[0].goods.current_page&&data[0].goods.data.last_page){
          this.setData({
            page:data[0].goods.data.current_page,
            total_page:data[0].goods.data.last_page,
          })
        }
        console.log(this.data.page,"page",this.data.total_page);
        this.initElementHeight()
      }
    })
  },
  initElementHeight(){
    const query = wx.createSelectorQuery();
    query.select(".section").boundingClientRect(rect => {
      if (rect && rect.height) {
        let height = rect.height + 'px';

        this.setData({
          order_height: height
        })

      }
    }).exec();

    query.selectAll(".award-bottom-item").boundingClientRect(rect => {
       let width = 0;
       console.log(rect,'rect========')
       rect.forEach(item=>{
         width+=item.width;
       })
       this.setData({_scroll_width:width+'px'})
    }).exec();
    query.selectAll(".coupon-award-bottom-item").boundingClientRect(rect => {
      let width = 0;
      console.log(rect,'rect========1111111111')
      if(rect&&rect.length==1){
        if(rect[0].width>=327){
          width = rect[0].width;
        }else{
          width =  this.data._scroll_width;
        }
      }else{
        rect.forEach(item=>{
          width+=item.width;
        })
      }
      console.log(width)
      this.setData({_scroll_width:width+'px'})
   }).exec();
  },
  getGoods(page) {
    app._getNetWork({
      url: app.getNetAddresss("payment.success-page.goods-page"),
      data: {
        order_id: this.data.order_id,
        page
      },
      success:resdata=>{
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result !== 1) {
          return app.tips(msg);
        }
        this.setData({
          page:data.current_page,
          total_page:data.last_page
        })
        console.log(data,"data===");
        if(data.current_page==1){
          this.setData({goods_list:data.data})
        }else{
          let _arr = this.data.goods_list.concat(data.data);
          this.setData({goods_list:_arr})

        }


        this.initElementHeight()

      }
    })

  },

  bindChangeBannerIndex(evt) {
    let index = evt.detail.current;
    this.setData({
      order_id:this.data.list[index].order_info.order_id,
      page:1,
      total_page:this.data.list[index].goods.last_page,
    });
    const SelectorQuery = wx.createSelectorQuery();
    SelectorQuery.selectAll(".section").boundingClientRect();
    SelectorQuery.exec(res => {
      let height = res[0][index].height + 'px';
      this.setData({
        order_height: height
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.page>=this.data.total_page){
      return;
    }
    let page = this.data.page+1;
    console.log(page,"page===");
    this.getGoods(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: this.data.title,
      imageUrl: this.data.imageUrl,
      path: this.data.path
    }
  }
})